﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System.Text;
using System.Windows.Forms;
using NetJsWire.JsonRpcHandler;
using NetJsWire.JsonRpcHandler.Attributes;
using NetJsWire.Widgets.Core;
using NetJsWire.Widgets.Renderers.Core;
using NetJsWire.Widgets.Ui.Layout;

namespace NetJsWire.Widgets.Renderers.Form
{
    [ClassName( "Ext.ux.NDateField" )]
    internal class DateField : Widget, IRenderer
    {
        //protected new string _INPUT = "{0}.on(\"change\", function(tf, newValue, oldValue) {{SendToServer(\"{0}\", \"input\", \"{1}\", tf.isValid() ? [tf.getValue().format(tf.format), true] : [tf.getRawValue(), false] );}});";
        protected string _INPUT = string.Empty;
        protected string _INPUT_CLIENT_NAME = "input";

        public DateField( object baseObject )
            : base( baseObject )
        {
        }

        #region IRenderer Members

        public override void Render()
        {
            var sb = new StringBuilder();
            if ( !Rendered )
            {
                if ( BaseParent is IWidgetContainer )
                {
                    SetAttribute( ControlNode, "isInContainer", true );
                    SetConstructionProperty( "isInContainer", true );

                    if ( !string.IsNullOrEmpty( ( ( Ui.Form.DateField ) Base ).FieldLabel ) &&
                         ( BaseParent as IWidgetContainer ).Layout == LayoutType.Form )
                        SetConstructionProperty( "fieldLabel", ( ( Ui.Form.DateField ) Base ).FieldLabel );
                }
                else
                {
                    SetConstructionProperty( "sendUpdates", true );
                    SetStyleProperty( "width", ( ( Control ) Base ).Size.Width );
                    SetStyleProperty( "position", "absolute" );
                    SetConstructionProperty( "renderTo", ClientParentToRender );
                }

                EnsureParentNodePosition();

                if ( ( ( Ui.Form.DateField ) Base ).SendUpdates )
                    SetConstructionProperty( "sendUpdates", true );

                //_constructorParameters = "\"" + ClientID + "\"";
                SetConstructionProperty( "format", "d/m/Y" );

                SetConstructionProperty( "value", ( ( Control ) Base ).Text );
                SetConstructionProperty( "width", ( ( Control ) Base ).Width );
                SetConstructionProperty( "tabIndex", ( ( Control ) Base ).TabIndex );
                SetConstructionProperty( "disabledClass", "disabled-tf" );
                SetConstructionProperty( "maxValue", ( ( Ui.Form.DateField ) Base ).MaxDate );
                //TODO: Check: ¿Why i have to add one day to min?
                SetConstructionProperty( "minValue", ( ( Ui.Form.DateField ) Base ).MinDate.AddDays( 1 ) );
            }
            base.Render();
        }

        #endregion
    }
}